﻿using Estsh.Core.Wms.IServices;
using Estsh.Core.Model.Result;
using Estsh.Core.Util;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Estsh.Core.Models;
using Microsoft.AspNetCore.Authorization;
using Estsh.Core.Controllers;

namespace Estsh.Core.Web.Areas.Wms.Controllers.Login
{
    [Route("wms/[controller]")]
    [ApiController]
    public class LoginController : WmsBaseController
    {
        private ILoginService service;

        // GET: LoginController
        public LoginController(ILoginService _service)
        {
            service = _service;
        }

        [HttpPost("Login")]
        [AllowAnonymousAttribute]
        public IActionResult Login(Newtonsoft.Json.Linq.JObject jobj)
        {
            WmsResponseResult result = new WmsResponseResult();
            try
            {
                string userNo = jobj["userNo"].ToString();
                string userPwd = jobj["userPwd"].ToString();
                string factoryId = jobj["factoryId"].ToString();
                string factoryName = jobj["factoryName"].ToString();

                List<SysEmp> obj = service.RunLogin(userNo, userPwd, factoryId, factoryName);

                if (obj.Count > 0)
                {
                    UserToken userToken = new UserToken { loginId = obj[0].EmpId.ToString(), loginNo = obj[0].EmpNo.ToString(), loginName = obj[0].EmpName.ToString(), loginTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), factoryId = obj[0].FactoryId.ToString() };
                    result.Success = true;
                    result.Data = userToken;
                }
                else
                {  
                    result.Success = false;
                    result.Msg = "账号密码有误，请重新尝试！";
                }

                
                
            }
            catch (Exception ex)
            {
                result.Msg = ex.Message;
            }
            return Json(result);

        }

        /// <summary>
        /// 获取工厂信息
        /// </summary>
        /// <returns></returns>
        [HttpPost("FactoryInfo")]
        [AllowAnonymousAttribute]
        public IActionResult FactoryInfo()
        {
            WmsResponseResult result = new WmsResponseResult();
            try
            {
                List<SysFactory> obj = service.GetFactoryInfo();

                result.Success = true;
                result.Data=obj;
            }
            catch (Exception ex)
            {
                result.Msg=ex.Message;
            }
            return Json(result);
        }

        [HttpPost("GetFactoryInfo")]
        public IActionResult GetFactoryInfo(Newtonsoft.Json.Linq.JObject jobj)
        {
            WmsResponseResult result = new WmsResponseResult();
            try
            {
                string factory_id = jobj["factoryId"].ToString();
                List<SysFactory> obj = service.GetFactoryInfo(factory_id);

                result.Success = true;
                result.Data = obj;
            }
            catch (Exception ex)
            {
                result.Msg = ex.Message;
            }
            return Json(result);
        }

        [HttpPost("GetUserInfo")]
        public IActionResult GetUserInfo(string userNo)
        {
            List<SysEmp> obj = service.GetUserInfo(userNo);
            return Json(obj);
        }

        [HttpPost("GetRole")]
        public IActionResult GetRole(Newtonsoft.Json.Linq.JObject jobj)
        {
            WmsResponseResult result = new WmsResponseResult();
            try
            {
                string userNo = jobj["userNo"].ToString();
                List<GetMenuList> resultList = service.GetRole(userNo);

                result.Success=true;
                result.Data= resultList;
            }
            catch (Exception ex)
            {
                result.Msg = ex.Message;
            } 
            return Json(result);
        }

        [HttpPost("GetRoleName")]
        public IActionResult GetRoleName(Newtonsoft.Json.Linq.JObject jobj)
        {
            WmsResponseResult result = new WmsResponseResult();
            try
            {
                string loginId = jobj["loginId"].ToString();
                List<SysEmp> resultList = service.GetRoleName(loginId);

                result.Success = true;
                result.Data = resultList;
            }
            catch (Exception ex)
            {
                result.Msg = ex.Message;
            }
            return Json(result);
        }
    }


}
